#!/usr/bin/env ruby
require "rexml/document"
include REXML

def pp (ugly)
  formatter = REXML::Formatters::Pretty.new(2)
  formatter.compact = true
  formatter.write(ugly, $stdout)
  puts
end

service_document = Document.new `scripts/api/data-deposit/service-document`
regex = 'peteTop'
col1 = nil
XPath.each(service_document, "//collection/@href") { |href|
    #if href.to_s.match(/col1/)
    if href.to_s.match(/#{regex}/)
        col1 = href
    end
}

if (!col1)
    puts "Hmm. We expected to find #{regex}"
    exit 1
end

puts "GET of #{col1}"
feed_of_studies = Document.new `scripts/api/data-deposit/get #{col1}`
#pp(feed_of_studies)

first = XPath.first(feed_of_studies, "//entry")
if (!first)
    puts "Have you created any datasets in #{col1} ?"
    exit 1
end
#pp(first)
id = XPath.first(first, "//id").text
puts "GET of #{id}"
entry = Document.new `scripts/api/data-deposit/get #{id}`
permalink = XPath.first(entry, "//link[@rel='alternate']/@href").to_s
permalink_last = permalink.split('/')[-1]
id_last = id.split('/')[-1]
if (id_last.to_s != permalink_last.to_s)
    puts "WARNING: mismatch between dataset id (#{id_last}) and permalink (#{permalink_last}): https://github.com/IQSS/dataverse/issues/758"
end
#  <link href='http://dx.doi.org/10.5072/FK2/12' rel='alternate'/>
#pp(entry)
#edit = XPath.first(entry, "//[@rel='edit']")
#puts edit
statement_link = XPath.first(entry, "//link[@rel='http://purl.org/net/sword/terms/statement']/@href")
puts "GET of #{statement_link}"
statement = Document.new `scripts/api/data-deposit/get #{statement_link}`
#pp(statement)
state = XPath.first(statement, "//category[@term='latestVersionState']").text
puts state
